<template>
	<view>

		<form>	
				<view class="cu-form-group margin-top">
					<view class="title">主题</view>
					<input  v-model="titleName" name="input"></input>
				</view>
			<view class="cu-form-group margin-top">
				<view class="title">币名</view>
				<input placeholder="" v-model="coinName" name="input"></input>
			</view>
			<view class="cu-form-group margin-top">
				<view class="title">总资产</view>
				<input  type="number" v-model="allProperty" name="input"></input> 
			</view>			
			<view class="cu-form-group margin-top">
				<view class="title">方向 </view>			 
			 <radio-group class="uni-flex uni-row radio-group" @change="dkDirect" style="flex-wrap: wrap">
			        <radio value="1" :checked="isBuy==1"  color="#00FF00" style="margin-right: 15px" class="radio r">多
			        </radio>
			        <radio value="0" :checked="isBuy==0"  style="margin-right: 15px" color="#FF0000" class="radio r1">空</radio>
			      </radio-group>
			</view>
			
			<view class="cu-form-group margin-top">
				<view class="title">买入价</view>
				<input  type="number" v-model="buyPrice" name="input"></input> 
			</view> 
			<view class="cu-form-group margin-top">
				<view class="title">止损价</view>
				<input  type="number" v-model="stopLossPrice" @blur="calc2lose"  name="input"></input> 
			</view>
			
			<view class="cu-form-group margin-top">
				<view class="title">购买量</view>
				<input  type="number" v-model="buyNum"  name="input"></input> 
			</view>
			
		
			<view class="cu-form-group margin-top">
				<view class="title">2%损失可开 {{lose2Num}}</view>
			</view>
		 
		
		<view class="cu-form-group margin-top">
			<view class="title">目标价格</view>
			<input  type="number" v-model="destPrice" @blur="calcykb" name="input"></input> 
			盈亏比：<label  >{{ykb}}</label>
		</view>
		
		<view class="cu-form-group margin-top">
			<view class="title">买入理由计划</view>
			<textarea maxlength="1000" v-model="buyReason"    ></textarea>
		</view>
		
		<view class="cu-form-group margin-top">
			<view class="title">所有调整</view>
			<textarea maxlength="1000"  v-model="ALLadjust"   ></textarea>
		</view>
		
		<view class="cu-form-group margin-top">
			<view class="title">实际卖出价</view>
			<input  type="number" v-model="sellPrice" name="input"></input> 
		</view>
		
		<view class="cu-form-group margin-top">
			<view class="title">最终盈亏</view>
			<input  type="number" v-model="profit" name="input"></input> 
		</view>
		
			
			<view class="cu-form-group">
				<view class="title">卖出日期</view>
			<picker mode="date" :value="CloseTime" start="2024-01-01" end="2035-01-01" @change="SYXChange">
				<view class="picker">			 
							{{CloseTime=='2024-01-01'?'请选择':CloseTime}}
				</view>
			</picker>
			</view>
			
			
			<view class="cu-form-group margin-top">
				<view class="title">总结</view>
				<textarea maxlength="-1" :disabled="modalName!=null" placeholder="显示信息"></textarea>
			</view>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					图片上传
				</view>
				<view class="action">
					{{imgList.length}}/9
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<10">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>		
			<view class="flex margin-top">
				<view class="cu-progress round">
				<progress :percent="percent" strock-width="25" ></progress>
				</view>
			</view>
			 <view class="padding flex flex-direction">
			 	<button class="cu-btn bg-red margin-tb-sm lg" @click="SubmitForm">提交</button>
			 </view>
		 <view >
			
		</view>
	
		
		</form>
	</view>
</template>

<script>
	 import service from '../../service.js';
	 var _self;
	export default {
		data() {
			return {
				titleName:'',
				coinName:'',
				allProperty:null,
				buyPrice:null,
				stopLossPrice:null,
				sellPrice:null,
				isBuy:1,
				buyNum:null,
				destPrice:null,
				buyReason:null,
				firstImg:'',
				ALLadjust:'',
				profit:null,
				CloseTime: '2024-01-01',
				imgList: [],
				upimgList: [],
				modalName: null,
				hasT:false,
				percent:0,
				lose2Num:0,
				ykb:0
			};
		},
		onLoad() {
			_self=this;
			this.inidata();
		},
		methods: { 
			inidata(){
				service.referToken(); //刷新，并判断是否要登录 
			},
			calc2lose( ) {
				let lose2Assert=this.allProperty*0.02;
					this.lose2Num=	lose2Assert/ Math.abs(this.buyPrice-this.stopLossPrice)  ;
					this.buyNum=this.lose2Num.toFixed(5);
			},	
			calcykb( ) {
			if(this.isBuy==1){
				//多
				 
					 let yinli=  this.destPrice-this.buyPrice;
					 let kuisun= this.buyPrice-this.stopLossPrice;					 
					 this.ykb=(yinli/kuisun).toFixed(5);
			}
			else {
				let yinli=  this.buyPrice-this.destPrice;
				let kuisun= this.stopLossPrice-this.buyPrice;
				this.ykb=  (yinli/kuisun).toFixed(5);
			}
			 
			},	
			
			SYXChange(e) {
				this.CloseTime = e.detail.value
			},
		 dkDirect(e) {
			 console.log(e.detail.value);
		 	this.isBuy = e.detail.value
		 },
			ChooseImage() {
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除图片',
					content: '确定要删除这张图片吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			}
			 
			,SubmitForm(){
			
				if(this.showPrice==""){
				 	uni.showToast({title:"请填写显示卖价"});
				 	return;
				}
				 if(_self.imgList.length<1){
				  	uni.showToast({title:"请上传图片"});
				  	return;
				 } 
			
				uni.showLoading({
					  title: '上传中'
				});
				this.upimg(0); //上传图片和提交数据
				 
			
				
				
			}
			,upimg(i){  
				if(i<_self.imgList.length){
					uni.uploadFile({
					         // 需要上传的地址
					         url: service.BaseUrl+'api/ApiTrader/UpLoadImg',
					         // filePath  需要上传的文件
					         filePath: _self.imgList[i],
					         name: 'file',
					         success(res1) {
					             // 显示上传信息
								 var imgres= JSON.parse(res1.data);
								 _self.upimgList.push(imgres.Data);
								 console.log(imgres.Data);
								 _self.percent =  (( i+1)*100)/_self.imgList.length ;					             
								 console.log( (( i+1)*100)+" "+_self.imgList.length +"="+_self.percent);
					       	 _self.upimg(i+1);
							 }
					     }); 
									
				}
				else{
					 var user=service.getUsers();
					uni.request({
						header:{ 'Authorization' : 'Basic '+service.getUsers().Token},
						method: 'POST',
						url: service.BaseUrl+'api/ApiTrader/AddCar',
						data: {
							  titleName  :_self.titleName,
							  coinName  :_self.coinName,
							  allProperty  :_self.allProperty,
							  buyPrice  :_self.buyPrice,
							  stopLossPrice  :_self.stopLossPrice,
							  sellPrice  :_self.sellPrice,
							  isBuy  :_self.isBuy,
							  buyNum  :_self.buyNum,
							  destPrice  :_self.destPrice,
							  buyReason  :_self.buyReason,
							  firstImg  :_self.firstImg,
							  ALLadjust  :_self.ALLadjust,
							  profit  :_self.profit,
							  createtime  :_self.createtime,
							  Modifytime  :_self.Modifytime,
							  CreateUserID  :_self.CreateUserID,
							  ModifyUserID  :_self.ModifyUserID,
							  CloseTime  :_self.CloseTime,
							  ykb:_self.ykb,
						    
							Carimgs:_self.upimgList,
							   
							CreateUserID:user.Userid,
							CreateUser: user.UserName,
							Token:service.getUsers().Token
							
						},
						success: res => {
							if (res.data.Code==1) {
								uni.hideLoading();
								uni.showModal({
								    title: '完成',
								    content: '添加成功',
									 showCancel:false,
								    success: (res) => {
								        if (res.confirm) { 
								                uni.reLaunch({url:"../index/index"}); 
								        } 
								    }});
							}else{
								uni.showToast({
									title:res.data.Msg
								})
								
							}
						}
						
					});
					
				}
				   
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
	
</style>
